// pages/user/user.js
import { Base } from '../../utils/base.js';

const app = getApp()
const base = new Base()
const upload = '/wxserver/imgUpload/upload'
const updateUser = '/purchase/user/updateUser'

Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断用户是否登录
    // let userInfo = app.globalData.userInfo;
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo){
      this.setData({
        userInfo: userInfo
      })
    }
   
  },
  updateUserInfo(){
    let userInfo = wx.getStorageSync('userInfo')

    if (userInfo) {
      this.setData({
        userInfo: userInfo
      })
    }
   
  },
  onShow:function(){
    this.getLogin();
    this.getCart();
    // let userInfo = app.globalData.userInfo;
  },
  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
  },
  getLogin(){
    let loginClose = wx.getStorageSync('loginClose');
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    });
    if (!userInfo && !loginClose) {

      wx.navigateTo({
        url: '/pages/login/login'
      })
    } 
  },
  onLogin(){
    //登录
    // let userInfo = app.globalData.userInfo;
    let userInfo = wx.getStorageSync('userInfo')

    if (!userInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  uploadAvator(){
    let userInfo = this.data.userInfo
    let that = this
    wx.chooseImage({
        count:1,
        success: function (res) {
            var tempFilePaths = res.tempFilePaths
            console.log('tempFilePaths', tempFilePaths)
            wx.showLoading({
                title: '上传中',
            })
            base.request({
                url: upload,
                type: "GET",
                data:{
                    "wxUid": "",
                    "wxSid": ""
                },
                sCallback(token){
                    console.log('token', token)
                    wx.uploadFile({
                        url: 'https://up-z1.qiniup.com', //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData:{
                            token: token.uptoken
                        }   ,
                        success: function (res) {
                            console.log(res)
                            var data = JSON.parse(res.data)
                            let params = {
                                url: updateUser,
                                data: {
                                    "data": {
                                        "id": userInfo.id,
                                        "name": userInfo.name,
                                        "password": userInfo.password,
                                        "role": userInfo.roleId,
                                        "status": userInfo.status,
                                        "url": base.qiniuBaseUrl + data.key
                                    },
                                },
                                sCallback(res) {
                                    userInfo.url = base.qiniuBaseUrl + data.key
                                    wx.setStorageSync('userInfo', userInfo)
                                    that.setData({
                                        userInfo: userInfo
                                    })
                                    wx.hideLoading()
                                    wx.showToast({
                                        title: '上传成功',
                                        icon: 'success',
                                        duration: 2000,
                                        success() {
                                            console.log('success')
                                        }
                                    })
                                }   
                            }
                            base.request(params)
                        }
                    })
                }
            })
        }
    })
  },
  logout(){
      wx.showModal({
          title: '提示',
          content: '确认退出登录吗？',
          success: function (res) {
              if (res.confirm) {
                  console.log('用户点击确定')
                  wx.removeStorageSync('userInfo')
                //   wx.setStorageSync('loginStatus', false)
                  wx.showToast({
                      title: '退出登录成功',
                      icon: 'success',
                      duration: 2000,
                      success() {
                          console.log('success')
                          wx.reLaunch({
                              url: '/pages/home/home'
                          })
                          
                          wx.removeTabBarBadge({
                            index: 1
                          });
                      }
                  })
              } else if (res.cancel) {
                  console.log('用户点击取消')
              }
          }
      })
  },
  onPullDownRefresh:function(){
      let userInfo = wx.getStorageSync('userInfo')

      if (userInfo) {
          this.setData({
              userInfo: userInfo
          })
           wx.stopPullDownRefresh()
      }
  },
  //获取购物车数据
  getCart() {
    let that = this;

    let userInfo = wx.getStorageSync('userInfo');
    let storeNo = userInfo.storeNo;
    if (userInfo) {
      let parames = {
        "userId": '' + userInfo.id,
        "storeNo": '' + storeNo
      }
      //获取购物车总数

      let url = "/purchase/cart/get";
      base.commonRequest(url, parames, res => {
        if (res.status) {
          let total = 0;
          res.data.forEach((value, index) => {
            total += parseInt(value.goodsNum);
          })
          wx.showTabBarRedDot({
            index: 1
          })
          //设置购物车右上角红色点
          wx.setTabBarBadge({
            index: 1,
            text: '' + total,
          })

        }
      });


    }

  }
})